package pay

import (
	"context"
	"strconv"

	"gitee.com/liuxuezhan/ar-platform/assets-api/internal/logic/withdraw"
	"gitee.com/liuxuezhan/ar-platform/assets-api/internal/svc"
	"gitee.com/liuxuezhan/ar-platform/base-common/headInfo"
	"gitee.com/liuxuezhan/ar-platform/rpc-client/assetsClient/rpc"
	settingrpc "gitee.com/liuxuezhan/ar-platform/rpc-client/setting/rpc"
	"github.com/zeromicro/go-zero/core/logc"
	"github.com/zeromicro/go-zero/core/logx"
)

type HandleOrder struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}
type NotifyMessageInfo struct {
	OrderNo string `json:"orderNo"`
}

// bpay代收回调通知
func NewHandleOrderLogic(ctx context.Context, svcCtx *svc.ServiceContext) *HandleOrder {
	return &HandleOrder{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *HandleOrder) AddNotifitionMessage(message *NotifyMessageInfo) error {
	page, _ := l.svcCtx.AssetsRpc.PayRecharge.RechargeOrderListPage(l.ctx, &rpc.RechargeOrderListPageRequest{
		Param: &rpc.RechargeOrderRequest{OrderNo: message.OrderNo},
		Page:  &rpc.PageRequest{PageSize: 10, PageNo: 1},
	})

	_, err := l.svcCtx.CommonSettingRpc.AddNotificationMessage(l.ctx, &settingrpc.AddNotificationMessageToCReq{
		MessageType: 2,
		UserId:      headInfo.GetTokenUid(l.ctx),
		Language:    "ar",
		Value:       []string{convertToString(page.RechargeOrderList[0].Amount)},
		MerchantId:  withdraw.MerchantIdGet(l.svcCtx.CommonSettingRpc, l.ctx),
	})
	if err != nil {
		logc.Error(l.ctx, err)
		return nil
	}
	return nil
}
func convertToString(value float64) string {
	return strconv.FormatFloat(value, 'f', -1, 64)
}
